package com.aisino.api.config;

import com.aisino.api.filter.ApiRequestFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class FilterConfig {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Autowired
    private com.aisino.api.service.DevLogService devLogService;

    @Bean
    public FilterRegistrationBean<ApiRequestFilter> apiRequestFilter() {
        FilterRegistrationBean<ApiRequestFilter> registrationBean = new FilterRegistrationBean<>();
        
        // 手动创建并配置ApiRequestFilter实例
        ApiRequestFilter apiRequestFilter = new ApiRequestFilter();
        // 手动注入依赖
        apiRequestFilter.setJdbcTemplate(jdbcTemplate);
        apiRequestFilter.setDevLogService(devLogService);
        
        registrationBean.setFilter(apiRequestFilter);
        registrationBean.addUrlPatterns("/v1/*");
        registrationBean.setOrder(1); // 设置过滤器顺序
        return registrationBean;
    }
}